/* Geometry Pixel Shader */

#include "LightUtils.hlsl"

// Light Info
cbuffer LightInfo : register(b0)
{
	float3	LightPosition;
	float	LightRange;
	float3	LightDirection;
	float	LightIntensity;
	uint	LightType;
	float3	LightPadding;
};

// Material Info
cbuffer MaterialInfo : register(b1)
{
	float3	CameraPosition;
	float	Reflection;
	float3	DiffuseColor;
	float	Specular;
	float3	SpecularColor;
	float	SpecularPower;
	float3	AmbientColor;
	float	Opacity;
	float	AmbientStrength;
	float3	Padding;
}

// Reflection Cubemap
TextureCube<float4> g_ReflectionTexture		: register(t0);
SamplerState g_ReflectionSampler			: register(s0);

// Pixel Input
struct PixelInput
{
	float4 Position			: SV_POSITION;
	float4 WorldPosition	: WORLDPOSITION;
	float3 Normal			: NORMAL;
	float2 TexCoord			: TEXCOORD0;
};

// Pixel Output
struct PixelOutput
{
	float4 Color			: SV_TARGET0;
};

PixelOutput main(PixelInput input)
{
	PixelOutput output = (PixelOutput)0;

	// Light Info
	LightInfo light;
	light.Position = LightPosition;
	light.Range = LightRange;
	light.Direction = LightDirection;
	light.Intensity = LightIntensity;
	light.LightType = LightType;

	// Calculate Diffuse Term
	float diffuseTerm = CalculateDiffuseTerm(light, input.WorldPosition, input.Normal);
	// Calculate Specular Term
	float specularTerm = CalculateSpecularTerm(light, input.WorldPosition, input.Normal, CameraPosition, SpecularPower);
	// Calclate Reflection Vector
	float3 reflectionVector = CalculateReflectionVector(CameraPosition, input.WorldPosition, input.Normal);

	// Sample the Reflection Color
	float3 reflectColor = g_ReflectionTexture.Sample(g_ReflectionSampler, reflectionVector).xyz;
	float3 diffuseColor = DiffuseColor * diffuseTerm;
	float3 albedoColor = lerp(diffuseColor, reflectColor, Reflection);

	output.Color = float4(AmbientColor, 1.0f) * AmbientStrength;
	output.Color += float4(SpecularColor, 1.0f) * specularTerm * Specular;
	output.Color += float4(albedoColor, 1.0f);
	output.Color.w = Opacity;

	//output.Color = float4(input.Normal.z, 0.0f, 0.0f, 1.0f);

	return output;
}